import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib as mpl
from io import StringIO

mpl.rcParams['pdf.fonttype'] = 42
mpl.rcParams['ps.fonttype'] = 42
mpl.rcParams['axes.linewidth'] = 1.5
mpl.rcParams['lines.linewidth'] = 3
mpl.rcParams['lines.markersize'] = 10
mpl.rcParams['font.size'] = 15
mpl.rcParams['xtick.labelsize'] = 14
mpl.rcParams['ytick.labelsize'] = 16
mpl.rcParams['legend.fontsize'] = 14
mpl.rcParams['legend.framealpha'] = 0
mpl.rcParams['legend.borderpad'] = 0.1
mpl.rcParams["axes.labelweight"] = "bold"
mpl.rcParams["axes.labelsize"] = 14
mpl.rcParams['font.family'] = 'sans-serif'
mpl.rcParams['figure.figsize'] = (9, 4)
# mpl.rc("font",family='MicroSoft YaHei',weight="bold")

csv = '''Bandwidth,System,Time,Mem
50,Socket,17.005
50,Socker,8.581
60,Socket,13.882
60,Socker,8.787
70,Socket,12.101
70,Socker,8.364
80,Socket,10.491
80,Socker,8.36
90,Socket,9.227
90,Socker,8.303
100,Socket,8.242,124.664
100,Socker,8.303,136.767
110,Socket,7.829,125.231
110,Socker,7.844,136.443
120,Socket,6.92,125.372
120,Socker,6.992,136.099
130,Socket,6.406,125.878
130,Socker,6.411,136.55
140,Socket,5.814,125.498
140,Socker,5.833,138.012
150,Socket,5.666,126.329
150,Socker,5.702,137.936
'''

df = pd.read_csv(StringIO(csv), sep=",")
print(df)

def plot_size():
  sns.barplot(x = 'Network',
              y = 'Size',
              hue = 'System',
              data = df)
  plt.ylabel('Max label size')
  plt.xlabel('Network')
  plt.legend().set_title(None)
  # plt.yscale('log')
  plt.tight_layout(pad=0.1)
  plt.savefig('./figures/dpg/eval-size.pdf')
  # Show the plot
  plt.show()
  
def plot_time():
  d = df.loc[df['Time'].isna() == False]
  sns.lineplot(x = 'Bandwidth',
              y = 'Time',
              hue = 'System',
              markers=True,
              style='System',
              data = d)
  plt.ylabel('Time (s)')
  plt.xlabel('S1-S2 Bandwidth (mbps)')
  plt.legend().set_title(None)
  plt.xticks([i * 10 for i in range(5,16)])
  # plt.xlim(50, 150)
  # plt.yscale('log')
  plt.tight_layout(pad=0.1)
  plt.savefig('./figures/nai/eval-time.pdf')
  # Show the plot
  plt.show()
  
def plot_time_overhead():
  mpl.rcParams['figure.figsize'] = (4, 4)
  d = df.loc[df['Bandwidth'] >= 100]
  sns.barplot(x = 'Bandwidth',
              y = 'Time',
              hue = 'System',
              data = d)
  plt.ylabel('Time (s)')
  plt.xlabel('S1-S2 Bandwidth (mbps)')
  plt.legend().set_title(None)
  # plt.xticks([i * 10 for i in range(5,16)])
  # plt.xlim(50, 150)
  # plt.yscale('log')
  plt.tight_layout(pad=0.1)
  plt.savefig('./figures/nai/eval-time-overhead.pdf')
  # Show the plot
  plt.show()
  
def plot_mem_overhead():
  mpl.rcParams['figure.figsize'] = (4, 4)
  d = df.loc[df['Bandwidth'] >= 100]
  sns.barplot(x = 'Bandwidth',
              y = 'Mem',
              hue = 'System',
              data = d)
  plt.ylabel('Memory (MB)')
  plt.xlabel('S1-S2 Bandwidth (mbps)')
  plt.legend().set_title(None)
  # plt.xticks([i * 10 for i in range(5,16)])
  # plt.xlim(50, 150)
  # plt.yscale('log')
  plt.ylim(0, 180)
  plt.tight_layout(pad=0.1)
  plt.savefig('./figures/nai/eval-mem-overhead.pdf')
  # Show the plot
  plt.show()


def stat():
  socket_time_all = df.loc[df['System'] == 'Socket']['Time'].tolist()
  socket_time = df.loc[df['System'] == 'Socket']['Time'].tolist()[5:]
  socket_mem = df.loc[df['System'] == 'Socket']['Mem'].tolist()[5:]
  socker_time_all = df.loc[df['System'] == 'Socker']['Time'].tolist()
  socker_time = df.loc[df['System'] == 'Socker']['Time'].tolist()[5:]
  socker_mem = df.loc[df['System'] == 'Socker']['Mem'].tolist()[5:]
  
  # ratio = [(i-j)/i for i,j in zip(baselines, fusionfixed)]
  # ratio = [(i-j)/i for i,j in zip(baselines, fusionvar)]
  # ratio = [(i-j)/i for i,j in zip(fusionps, fusionfixed)]
  # ratio = [(i-j)/i for i,j in zip(fusionps, fusionvar)]
  print((sum(socket_time_all) - sum(socker_time_all)) / sum(socket_time_all))
  print((sum(socker_mem) - sum(socket_mem)) / sum(socket_mem))
  print((sum(socket_time)) / sum(socker_time))

stat()
# plot_size()
# plot_time_overhead()
# plot_mem_overhead()

  